﻿<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Quản lí bộ test</title>
</head>
<body>
<?php 
require_once("../include/db_info.inc.php"); 
require_once("admin-header.php");
require_once("../maincore.php");
if (!defined("THNOJ")) die("Outside of the site");
if(!isset($_GET['pid'])) die("Quay lại");
$pid=intval($_GET['pid']);
$sql="SELECT * FROM `problem` WHERE `problem_id`=".$pid." and author_id='$myid'";
$result=mysql_query($sql);
if (mysql_num_rows($result)<1) die("Không thể sửa");
$row=mysql_fetch_object($result);
$basedir=$OJ_DATA."/$row->problem_id";
?>
<center><h1>Sửa bài - quản lí bộ test</h1></center>

<?php
if (isset($_GET['act'])) {
	if ($_GET['act']=="del") {
		if (isset($_GET['tid'])) {
			$tid=intval($_GET['tid']);
			$sql="delete from ctd_test where id=$tid and user_id='$myid'";
			$ret = mysql_query($sql);
			if (file_exists($basedir."/$tid.in")) 
				if (unlink($basedir."/$tid.in"))
					echo "Đã xóa file input.";
			if (file_exists($basedir."/$tid.out")) 
				if (unlink($basedir."/$tid.out"))
					echo "Đã xóa file output.";
			
		} else {
			echo "Chỉ định bộ test cần xóa.";
		}
	} else if ($_GET['act']=="add") {
		if (isset($_POST['addtest'])) {
			$test_input=($_POST['test_input']);
			$test_output=($_POST['test_output']);
			$sql = "insert into ctd_test (`problem`,`input`,`output`,`user_id`) VALUE ($pid,'".$test_input."','".$test_output."','$myid')";
			$ret=mysql_query($sql);
			$test_id = mysql_insert_id();
			if(!file_exists($basedir)) 
				mkdir($basedir); 

			$fp=fopen($basedir."/$test_id.in","w");
			fputs($fp,preg_replace("(\r\n)","\n",$test_input));
			fclose($fp);
			$fp=fopen($basedir."/$test_id.out","w");
			fputs($fp,preg_replace("(\r\n)","\n",$test_output));
			fclose($fp);
		}
		redirect(MYSELF."?pid=".$_GET['pid'],2);
	} else if ($_GET['act']=="edit") {
		$tid=intval($_GET['tid']);
		if (isset($_POST['sua'])) {
			$input=($_POST['test_input']);
			$output=($_POST['test_output']);
			$sql="update ctd_test set `input`='$input', `output`='$output' where id=$tid and user_id='$myid'";
			$result = mysql_query($sql);
			$fp=fopen($basedir."/$tid.in","w");
			fputs($fp,preg_replace("(\r\n)","\n",$input));
			fclose($fp);
			$fp=fopen($basedir."/$tid.out","w");
			fputs($fp,preg_replace("(\r\n)","\n",$output));
			fclose($fp);
			redirect(MYSELF."?pid=".$_GET['pid'],2);
		} else {
			echo "<h2>Sửa bộ test $tid:</h2>";
			$sql="select * from ctd_test where id=$tid and user_id='$myid'";
			$result = mysql_query($sql);
			if (mysql_num_rows($result)) {
				$row = mysql_fetch_array($result);
				echo "<form method='POST' action='".MYSELF."?act=edit&pid=$pid&tid=$tid'>";
				echo "<table>";
				echo "<tr><td>Input</td><td><textarea rows='8' name='test_input' cols='60'>".$row['input']."</textarea></td></tr>";
				echo "<tr><td>Output</td><td><textarea rows='2' name='test_output' cols='60'>".$row['output']."</textarea></td></tr></table>";
				echo "<br><input type='submit' name='sua' value='Sửa'></form>";
			} else {
				echo "Không có bộ test.";
			}
		}
	}
} else {

	echo "<p>Mã đề bài: $row->problem_id</p>
	<p>Tiêu đề: ".htmlspecialchars($row->title)."</p>
	<p><a href='test_gen.php?id=$row->problem_id'>Sinh lại các file test</a></p>
	<p>Các bộ test:</p>";
	$test_id=1;
	$sql="select * from ctd_test where problem=".$pid;
	$ret = mysql_query($sql);
	while ($row = mysql_fetch_array($ret)) {
		echo "Test <b>$test_id</b> (".$row['id']."): [<a href='".MYSELF."?act=del&pid=$pid&tid=".$row['id']."'>Xóa bộ test này</a>] [<a href='".MYSELF."?act=edit&pid=$pid&tid=".$row['id']."'>Sửa bộ test này</a>]<br>";
		echo "<table>";
		echo "<tr align='left'><td>INPUT</td><td><div class=content><pre>".$row['input']."</pre></div></td></tr>";
		echo "<tr align='left'><td>OUTPUT</td><td><div class=content><pre>".$row['output']."</pre></div></td></tr>";
		echo "</table>";
		$test_id++;
	}
	/*
	while (file_exists($basedir."/test$test_id.in")) {
		echo "Test <b>$test_id</b> : [<a href='".MYSELF."?act=del&pid=$row->problem_id&tid=$test_id'>Xóa bộ test này</a>]<br>";
		echo "<table><tr align='center'><td>Input:</td><td>Output:</td></tr>";
		$fin = fopen($basedir."/test$test_id.in","r");
		$content = fread($fin,filesize($basedir."/test$test_id.in"));
		fclose($fin);
		echo "<tr align='left'><td><div class=content><pre>".$content."</pre></div></td>";
		$fout = fopen($basedir."/test$test_id.out","r");
		$content = fread($fout,filesize($basedir."/test$test_id.out"));
		echo "<td><div class=content><pre>".$content."</pre></div></td></tr></table>";
		fclose($fout);
		$test_id++;
		
	}*/
	echo "<h2>Thêm bộ test :</h2>";
	echo "<form method='POST' action='".MYSELF."?act=add&pid=$pid'>";
	echo "<table>";
	echo "<tr><td>Input:</td><td><textarea rows='8' name='test_input' cols='60'></textarea></td></tr>";
	echo "<tr><td>Output:</td><td><textarea rows='2' name='test_output' cols='60'></textarea></td></tr></table>";
	echo "<br><input type='submit' name='addtest' value='Thêm test'></form>";

}
require_once("../oj-footer.php");
?>


</body>
</html>

